package com.itheima.myiotest2;

import cn.hutool.core.io.FileUtil;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Random;

public class Test1 {
    public static void main(String[] args) throws IOException {

        //读取对象
        BufferedReader br = new BufferedReader(new FileReader("myiotest\\test.txt"));

        //读取文档并添加到集合，男女分开添加
        ArrayList<String> boyList = new ArrayList<>();
        ArrayList<String> girlList = new ArrayList<>();
        String line;
        while ((line = br.readLine()) != null) {
            //添加到集合
            if (line.contains("男")) {
                boyList.add(line);
            } else {
                girlList.add(line);
            }
        }
        br.close();

        //记录总人数
        int studentCount=boyList.size()+girlList.size();
        //System.out.println(list);

        //随机数据并输出名字
        Random r = new Random();

        //获取次数
        BufferedReader br2 = new BufferedReader(new FileReader("myiotest\\num.txt"));
        int num = br2.read()-'0';
        System.out.println(num);
        br2.close();

        //写入
        PrintWriter pw=new PrintWriter(new FileWriter("myiotest\\num.txt"));

        if(num==3){
            System.out.println("zhangsan");
            pw.println(++num);
            pw.close();
            System.exit(0);
        }

        //控制男女概率
        int j = r.nextInt(10) + 1;
        if (j <= 7) {
            //女生
            String girl = girlList.get(r.nextInt(girlList.size()));
            //吧被点过的删掉
            girlList.remove(girl);
            //覆盖文件
            FileUtil.writeLines(girlList,"myiotest\\test.txt", Charset.forName("utf-8"));

            System.out.println(girl.split("-")[0]);
            //girlCount++;
        } else {
            //男生
            String boy = boyList.get(r.nextInt(boyList.size()));
            boyList.remove(boy);
            FileUtil.writeLines(boyList,"myiotest\\test.txt", Charset.forName("utf-8"));
            System.out.println(boy.split("-")[0]);
            //boyCount++;
        }
        pw.println(++num);
        pw.close();

    }
}
